package com.exchange.controller.app.wallet;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import com.exchange.common.model.User;
import com.exchange.common.model.UserWallet;
import com.exchange.common.model.UserWalletFlow;
import com.exchange.constant.BizConst;
import com.exchange.interceptor.EnumError;
import com.jfinal.aop.Before;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;

public class WalletService {
	
	int pageSize = 20;
	
	public Ret details(User user) {
		List<UserWallet> list = UserWallet.dao.find("SELECT * FROM t_user_wallet WHERE userId = ? ", user.getId());
		return Ret.ok("walletList", list);
    }
	
	public Ret info(User user, String coin) {
		UserWallet wallet = UserWallet.dao.findFirst("SELECT * FROM t_user_wallet WHERE userId = ? AND coin = ? ", user.getId(), coin);
		return Ret.ok("coinWallet", wallet);
    }
	
	public Ret flow(User user, Integer pageNumber, Integer type, String dateFrom, String dateTo) {
		StringBuffer sqlExceptSelect = new StringBuffer("FROM t_user_wallet_flow WHERE userId = ? ");
		if(BeanUtil.isNotEmpty(type)) {
			sqlExceptSelect.append(" AND type = " + type);
		}
		if(StrUtil.isNotBlank(dateFrom)) {
			sqlExceptSelect.append(" AND createTime >= " + dateFrom);
		}
		if(StrUtil.isNotBlank(dateTo)) {
			sqlExceptSelect.append(" AND createTime <= " + dateTo);
		}
		sqlExceptSelect.append(" ORDER BY id ");
		
		Page<UserWalletFlow> walletFlowPage = UserWalletFlow.dao.paginate(pageNumber, pageSize, "select * ", sqlExceptSelect.toString(), user.getId());
        return Ret.ok("walletFlowPage", walletFlowPage);
    }
	
	@Before(Tx.class)
	public Ret transfer(User user, String coin, BigDecimal amount, String fromType, String toType) {		
		//获取用户此币种的钱包
		UserWallet wallet = UserWallet.dao.findFirst("SELECT * FROM t_user_wallet WHERE userId = ? AND coin = ? ", user.getId(), coin);
		UserWalletFlow walletFlow = new UserWalletFlow();
		
		if(StrUtil.equals(fromType, BizConst.WalletConst.WALLET_TYPE_OTC)) {
			walletFlow.setAddressFrom(BizConst.WalletConst.WALLET_TYPE_OTC);
			
			BigDecimal usable = wallet.getBalanceOTC();
			//判断是否可以划转
			if(NumberUtil.isGreater(amount, usable)){
		    	return Ret.fail("msg", EnumError.WALLET_INSUFFICIENT_FUNDS.getCode());// 余额不不足
		    }
			//执行划转
			wallet.setBalanceOTC(NumberUtil.sub(wallet.getBalanceOTC(), amount));
			if(StrUtil.equals(toType, BizConst.WalletConst.WALLET_TYPE_TRADE)) {
				walletFlow.setAddressTo(BizConst.WalletConst.WALLET_TYPE_TRADE);
		    	wallet.setBalance(NumberUtil.add(wallet.getBalance(),amount));
			}
		}
		if(StrUtil.equals(fromType, BizConst.WalletConst.WALLET_TYPE_TRADE)) {
			walletFlow.setAddressTo(BizConst.WalletConst.WALLET_TYPE_TRADE);
			BigDecimal usable = wallet.getBalance();
			//判断是否可以划转
			if(NumberUtil.isGreater(amount, usable)){
		    	return Ret.fail("msg", EnumError.WALLET_INSUFFICIENT_FUNDS.getCode());// 余额不不足
		    }
			//执行划转
			wallet.setBalance(NumberUtil.sub(wallet.getBalance(), amount));
			if(StrUtil.equals(toType, BizConst.WalletConst.WALLET_TYPE_OTC)) {
				walletFlow.setAddressFrom(BizConst.WalletConst.WALLET_TYPE_OTC);
		    	wallet.setBalanceOTC(NumberUtil.add(wallet.getBalanceOTC(),amount));
			}
		}
		wallet.update();
		//添加钱包变化流水
		walletFlow.setUserId(user.getId()).setType(BizConst.WalletConst.TRANSFER).setAmount(amount).setCoin(coin).setCreateTime(new Date()).setOriginator(user.getId());
		walletFlow.save();
       
        return Ret.ok();
    }
	
	
	
	
	
}
